var goodsModification = new Vue({
  el: "#app-gm",
  data: {
    //一级目录
    currentCategory1: "",
    currentCategory1_index: 0,
    currentCategory2_index: 0,
    //二级目录
    currentCategory2: "",
    currentCategory2_sets: [],
    //特殊分类
    currentSpcCategory: "",
    primaryCategory: [],
    secondCategory: [],
    specialCategory: [],
    originalChaoticInfo: [],
    //商品信息
    goods: [],
    //目录结构
    categoryTree: [],
    isSearching: false,
    currentSearchPattern: "",
    isModifying: []
  },
  computed: {
    isCategory2Valid: function() {
      return this.currentCategory2 === "暂无分类";
    }
  },
  methods: {
    //得到所有一级目录
    getAllCategory: function(init) {
      this.$http.get(SERVER_PORT + "requestCategory").then(
        response => {
          console.log(response.data.data);
          var originalChaoticInfo = response.data.data;
          goodsModification.originalChaoticInfo = Object.assign([], originalChaoticInfo);

          for (var index in originalChaoticInfo) {
            var primaryCategoryName = originalChaoticInfo[index].category1_Name;
            var secondCategoryNames = [];

            for (var sub_index in originalChaoticInfo[index].category2) {
              var secondCategoryName =
                originalChaoticInfo[index].category2[sub_index].name;
              secondCategoryNames.push(secondCategoryName);
            }

            goodsModification.categoryTree.push({
              primaryCategory: primaryCategoryName,
              secondCategory: secondCategoryNames
            });
          }

          console.log(this.categoryTree);
          goodsModification.currentCategory1 = goodsModification.categoryTree[0].primaryCategory;
          if (goodsModification.categoryTree[0].secondCategory.length === 0) {
            goodsModification.currentCategory2 = "暂无分类";
            goodsModification.goods = Object.assign([], []);
          } else {
            goodsModification.currentCategory2 = goodsModification.categoryTree[0].secondCategory[0];
            goodsModification.requestGoodsInCategory2(
              goodsModification.originalChaoticInfo[0].category2[0].category2_ID
            );
          }
          if (init !== undefined) goodsModification.onPriCateClick(init);
          //this.goods = Object.assign([], response.data.data);
          // 响应成功回调
        },
        response => {
          // 响应错误回调
        }
      );
    },

    requestGoodsInCategory2: function(id) {
      this.$http
        .post(
          SERVER_PORT + "requestGoodsInCategory2",
          {
            category2_ID: parseInt(id)
          },
          {
            emulateJSON: true
          }
        )
        .then(response => {
          //console.log(response.data.data);
          this.goods = Object.assign([], response.data.data);
          for (let i = 0; i < this.goods.length; i++) {
            this.isModifying.push(false);
          }
          if (this.currentSearchPattern !== "") {
            this.matchGoods = [];
            for (let good of this.goods) {
              for (let attr in good) {
                if (attr === "goods_ID") continue;
                if (good[attr].toString().match(this.currentSearchPattern)) {
                  this.matchGoods.push(good);
                  break;
                }
              }
            }
            this.goods = Object.assign([], this.matchGoods);
            this.isModifying = Object.assign([], []);
            for (let i = 0; i < this.matchGoods.length; i++) {
              this.isModifying.push(false);
            }
          }
          this.isSearching = false;
        });
    },

    //监听一级目录点击事件
    onPriCateClick: function(index) {
      this.currentCategory1 = this.categoryTree[index].primaryCategory;
      this.currentCategory2_sets = this.categoryTree[index].secondCategory;
      this.currentCategory1_index = index;
      this.currentCategory2_index = 0;

      if (this.categoryTree[index].secondCategory.length === 0) {
        this.currentCategory2 = "暂无分类";
        this.goods = Object.assign([], []);
      } else {
        this.currentCategory2 = this.categoryTree[index].secondCategory[0];
        this.requestGoodsInCategory2(
          this.originalChaoticInfo[index].category2[0].category2_ID
        );
      }
    },

    onSecCateClick: function(index) {
      this.currentCategory2 = this.categoryTree[this.currentCategory1_index]
          .secondCategory[index];
      this.currentCategory2_index = index;
      this.requestGoodsInCategory2(
        this.originalChaoticInfo[this.currentCategory1_index].category2[index]
          .category2_ID);
    },

    submit: function() {},

    deleteItem: function(index) {
      Ply.dialog("confirm", "确定删除?").done(function() {
        console.log("delete item " + index);
        Vue.http.post(SERVER_PORT + 'deleteGoods', {
          goods_ID: goodsModification.goods[index].goods_ID
        }, { emulateJSON: true }).then(response => {
          if (response.data.status === 1) {
            Ply.dialog('alert', '删除成功！');
            goodsModification.goods.splice(index, 1);
            goodsModification.isModifying.splice(index, 1);
          } else {
            Ply.dialog('alert', '删除失败，请重试！');
          }
        });
      });
    },

    performSearch: function() {
      this.isSearching = true;
      this.requestGoodsInCategory2(
        this.originalChaoticInfo[this.currentCategory1_index]
            .category2[this.currentCategory2_index].category2_ID
      );
    },

    //修改商品信息
    modifyItem:function(index){
      //console.log(this.goods[index].goods_ID)
      // Cookies.set("good_id",this.goods[index].goods_ID);
      window.localStorage.setItem("good_id",this.goods[index].goods_ID);
      window.location.href="./addGoods.html";
    }
  },

  beforeMount: function() {
    this.getAllCategory(0);
  }
});
